#include "setrect.h"
#include "ui_setrect.h"
#include<qdebug.h>
#include<QMessageBox>

setrect::setrect(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::setrect)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_QuitOnClose,false);
    this->setAttribute(Qt::WA_DeleteOnClose,false);
    ini=new QSettings("setrect.ini",QSettings::IniFormat);
    ml=new mylabel();
    ml->sr=this;
    sml=new mylabel();
    sml->sr=this;
    this->cle=nullptr;
    this->on_pushButton_reload_clicked();
}

setrect::~setrect()
{
    delete ui;
    delete ini;
    delete ml;
    delete sml;
}

void setrect::on_pushButton_ok_clicked()
{
    this->on_pushButton_save_clicked();
    this->accept();
}

void setrect::on_pushButton_cancel_clicked()
{
    this->reject();
}

void setrect::on_pushButton_save_clicked()
{
    ini->beginGroup("control");
    ini->setValue("rectnum",ui->lineEdit_rectnum->text());
    ini->setValue("sleeptime",ui->lineEdit_sleeptime->text());
    ini->endGroup();

    ini->beginGroup("rects");
    ini->setValue("rect1",ui->lineEdit_1->text());
    ini->setValue("rect2",ui->lineEdit_2->text());
    ini->setValue("rect3",ui->lineEdit_3->text());
    ini->setValue("rect4",ui->lineEdit_4->text());
    ini->setValue("rect5",ui->lineEdit_5->text());
    ini->setValue("rect6",ui->lineEdit_6->text());
    ini->setValue("rect7",ui->lineEdit_7->text());
    ini->setValue("rect8",ui->lineEdit_8->text());
    ini->setValue("rect9",ui->lineEdit_9->text());
    ini->setValue("rect10",ui->lineEdit_10->text());
    ini->setValue("rect11",ui->lineEdit_11->text());
    ini->endGroup();

    ini->beginGroup("sounds");
    ini->setValue("sound1",ui->lineEdit_sound_1->text());
    ini->setValue("sound2",ui->lineEdit_sound_2->text());
    ini->setValue("sound3",ui->lineEdit_sound_3->text());
    ini->setValue("sound4",ui->lineEdit_sound_4->text());
    ini->setValue("sound5",ui->lineEdit_sound_5->text());
    ini->setValue("sound6",ui->lineEdit_sound_6->text());
    ini->setValue("sound7",ui->lineEdit_sound_7->text());
    ini->setValue("sound8",ui->lineEdit_sound_8->text());
    ini->setValue("sound9",ui->lineEdit_sound_9->text());
    ini->setValue("sound10",ui->lineEdit_sound_10->text());
    ini->setValue("sound11",ui->lineEdit_sound_11->text());
    ini->endGroup();
}

void setrect::on_pushButton_reload_clicked()
{
    ini->beginGroup("control");
    ui->lineEdit_rectnum->setText(ini->value("rectnum").toString());
    ui->lineEdit_sleeptime->setText(ini->value("sleeptime").toString());
    ini->endGroup();

    ini->beginGroup("rects");
    ui->lineEdit_1->setText(ini->value("rect1").toString());
    ui->lineEdit_2->setText(ini->value("rect2").toString());
    ui->lineEdit_3->setText(ini->value("rect3").toString());
    ui->lineEdit_4->setText(ini->value("rect4").toString());
    ui->lineEdit_5->setText(ini->value("rect5").toString());
    ui->lineEdit_6->setText(ini->value("rect6").toString());
    ui->lineEdit_7->setText(ini->value("rect7").toString());
    ui->lineEdit_8->setText(ini->value("rect8").toString());
    ui->lineEdit_9->setText(ini->value("rect9").toString());
    ui->lineEdit_10->setText(ini->value("rect10").toString());
    ui->lineEdit_11->setText(ini->value("rect11").toString());
    ini->endGroup();

    ini->beginGroup("sounds");
    ui->lineEdit_sound_1->setText(ini->value("sound1").toString());
    ui->lineEdit_sound_2->setText(ini->value("sound2").toString());
    ui->lineEdit_sound_3->setText(ini->value("sound3").toString());
    ui->lineEdit_sound_4->setText(ini->value("sound4").toString());
    ui->lineEdit_sound_5->setText(ini->value("sound5").toString());
    ui->lineEdit_sound_6->setText(ini->value("sound6").toString());
    ui->lineEdit_sound_7->setText(ini->value("sound7").toString());
    ui->lineEdit_sound_8->setText(ini->value("sound8").toString());
    ui->lineEdit_sound_9->setText(ini->value("sound9").toString());
    ui->lineEdit_sound_10->setText(ini->value("sound10").toString());
    ui->lineEdit_sound_11->setText(ini->value("sound11").toString());
    ini->endGroup();
}

void setrect::on_pushButton_showrects_clicked()
{
    ml->rects[0][0]=ui->lineEdit_rectnum->text().toInt();
    for(int i=1;i<=11;i++)
    {
        QLineEdit *le=this->findChild<QLineEdit *>("lineEdit_"+QString::number(i));
        if(nullptr!=le){
                ml->rects[i][0]=le->text().section(',',0,0).toInt();
                ml->rects[i][1]=le->text().section(',',1,1).toInt();
                ml->rects[i][2]=le->text().section(',',2,2).toInt();
                ml->rects[i][3]=le->text().section(',',3,3).toInt();
//                qDebug()<<ml->rects[i][0]<<ml->rects[i][1]<<ml->rects[i][2]<<ml->rects[i][3]<<endl;
        }
    }
    this->hide();
    ml->showFullScreen();
}

void setrect::on_lineEdit_rectnum_textEdited(const QString &arg1)
{

}

void setrect::on_lineEdit_rectnum_textChanged(const QString &arg1)
{
    if(arg1.toInt()<1 || arg1.toInt()>11)QMessageBox::critical(this,"input error","rectmum must >=1 & <=11!");
}

void setrect::on_setrectButton_1_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_1;
}

void setrect::on_setrectButton_2_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_2;
}

void setrect::on_setrectButton_3_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_3;
}

void setrect::on_setrectButton_4_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_4;
}

void setrect::on_setrectButton_5_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_5;
}

void setrect::on_setrectButton_6_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_6;
}

void setrect::on_setrectButton_7_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_7;
}

void setrect::on_setrectButton_8_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_8;
}

void setrect::on_setrectButton_9_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_9;
}

void setrect::on_setrectButton_10_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_10;
}

void setrect::on_setrectButton_11_clicked()
{
    this->hide();
    sml->showFullScreen();
    cle= ui->lineEdit_11;
}
